<?php
namespace Crm\Table;

use Zend\Db\Sql\Select;
use Zend\Db\Sql\Where;
use Zend\Db\Sql\Expression;
use Zend\Db\Adapter\Adapter;

class ReturnOrderTable extends AbstractTable
{
    protected $table = 'hy_return_order';
    protected $id = 'order_id';
    
    
    public function getReturnOrderList($query = array())
    {

        $select = new Select(array('ro' =>$this->getTable()));
        
        $select->join(
            array('o' => 'hy_order'),
            'o.order_id = ro.order_id',
            array('order_no','org_id','org_name','user_id') ,
            $select::JOIN_LEFT
            );
        $select->join(
            array('roi' => 'hy_return_orderitem'),
            'roi.order_id = ro.order_id',
            array('amounts'=>new Expression('group_concat(roi.return_amount)')),
            $select::JOIN_LEFT
            );
        $select->join(
            array('oi' => 'hy_orderitem'),
            'oi.oi_id = roi.oi_id',
            array('products'=>new Expression('group_concat(oi.oi_name)')),
            $select::JOIN_LEFT
            );
        
        
        if(!empty($query['order_no'])) {
            $select->where(array('o.order_no' => $query['order_no']));
        }
        
        if(!empty($_SESSION['account']['org_id'])) {
            $select->where(array('o.org_id' => $_SESSION['account']['org_id']));
        }
        
        $select->order('ro.create_time DESC');
        $select->group('ro.order_id');
         
        return $this->fetchPaginate($select,$query);
        
    }
    public function updateStatus($order_id)
    {
        if(empty($order_id)){
            throw new \Exception("order_id field is invalid!");
        }
        $item = array(
            'status'=>2,
        );
        return $this->update($item,array('order_id'=>$order_id));
        
    }

}